package com.zhm.demo.modbus;

import com.ghgande.j2mod.modbus.Modbus;
import com.ghgande.j2mod.modbus.facade.ModbusSerialMaster;
import com.ghgande.j2mod.modbus.facade.ModbusTCPMaster;
import com.ghgande.j2mod.modbus.procimg.InputRegister;
import com.ghgande.j2mod.modbus.procimg.Register;
import com.ghgande.j2mod.modbus.util.BitVector;
import com.ghgande.j2mod.modbus.util.SerialParameters;

public class ModbusRTUClient {
    public static void main(String[] args) {
        try {
            SerialParameters serialParams = new SerialParameters();
            serialParams.setPortName("COM4"); // Windows串口名称，根据实际设备修改
            serialParams.setBaudRate(9600);   // 波特率
            serialParams.setDatabits(8);      // 数据位
            serialParams.setStopbits(1);      // 停止位
            serialParams.setParity("NONE");   // 校验位

            // 创建 ModbusTCPMaster 实例
            ModbusSerialMaster master = new ModbusSerialMaster(serialParams);
            master.connect();

            // 读取保持寄存器（假设从站地址为 1，寄存器地址从 0 开始，读取 10 个寄存器）

            InputRegister[] registers1 = master.readInputRegisters(1, 2, 1);

            // 打印读取到的寄存器值
            for (int i = 0; i < registers1.length; i++) {
                System.out.println("Holding Register1 " + (i) + " = " + registers1[i].getValue());
            }


            BitVector registers2 = master.readInputDiscretes(1, 1, 8);

            // 打印读取到的寄存器值

            System.out.println("Holding Register2 " + " = " + registers2.getBit(0));
            System.out.println("Holding Register2 " + " = " + registers2.getBit(1));
            System.out.println("Holding Register2 " + " = " + registers2.getBit(2));
            System.out.println("Holding Register2 " + " = " + registers2.getBit(3));
            System.out.println("Holding Register2 " + " = " + registers2.getBit(4));
            System.out.println("Holding Register2 " + " = " + registers2.getBit(5));
            System.out.println("Holding Register2 " + " = " + registers2.getBit(6));
            System.out.println("Holding Register2 " + " = " + registers2.getBit(7));


            BitVector registers3 = master.readCoils(1, 1, 8);
            System.out.println("Holding Register3 " + " = " + registers3.getBit(0));
            System.out.println("Holding Register3 " + " = " + registers3.getBit(1));
            System.out.println("Holding Register3 " + " = " + registers3.getBit(2));
            System.out.println("Holding Register3 " + " = " + registers3.getBit(3));
            System.out.println("Holding Register3 " + " = " + registers3.getBit(4));
            System.out.println("Holding Register3 " + " = " + registers3.getBit(5));
            System.out.println("Holding Register3 " + " = " + registers3.getBit(6));
            System.out.println("Holding Register3 " + " = " + registers3.getBit(7));


            Register[] registers = master.readMultipleRegisters(1, 0, 2);
            for (Register register : registers) {
                System.out.println("Holding Register4 " + " = " + register.getValue());
            }
            // 断开连接
            master.disconnect();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}